package java_9_16zy.Test_1;


public class Test_1 {
    public static String aaa = "aaa";
    public static String bbb = "bbb";

    public static void main(String[] args) {
    Thread t=new Thread(new Lock());
    Thread t1=new Thread(new Lock1());
    t.start();
    t1.start();
    }
}
class Lock implements Runnable{

    @Override
    public void run() {
        while (true){
            synchronized (Test_1.bbb){
                System.out.println(Thread.currentThread().getName()+"锁住了aaa");
                try {
                    Thread.sleep(1000);
                    synchronized (Test_1.aaa){
                        System.out.println(Thread.currentThread().getName()+"锁住了bbb");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
class Lock1 implements Runnable{

    @Override
    public void run() {
        while (true){
            synchronized (Test_1.aaa){
                System.out.println(Thread.currentThread().getName()+"锁住了aaa");
                try {
                    Thread.sleep(1000);
                    synchronized (Test_1.bbb){
                        System.out.println(Thread.currentThread()+"锁住了bbb");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}